package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.MerNotice;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.mernoticeform.InsertMerNoticeForm;
import com.example.wechattakeaway.form.mernoticeform.UpdateMerNoticeForm;
import com.example.wechattakeaway.mapper.MerNoticeMapper;
import com.example.wechattakeaway.service.MerNoticeService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
public class MerNoticeServiceImpl implements MerNoticeService {
    @Autowired
    MerNoticeMapper merNoticeMapper;
    //插入公告
    @Override
    public ResultVO insertMerNotice(InsertMerNoticeForm insertMerNoticeForm) {
        MerNotice merNotice=new MerNotice();
        merNotice.setIsShow(insertMerNoticeForm.getIsShow());
        merNotice.setNoticeContent(insertMerNoticeForm.getNoticeContent());
        merNotice.setNoticeTitle(insertMerNoticeForm.getNoticeTitle());
        merNotice.setMerchantId(insertMerNoticeForm.getMerchantId());
        try {

            merNoticeMapper.insert(merNotice);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }
    //修改公告
    @Override
    public ResultVO updateNotice(UpdateMerNoticeForm updateMerNoticeForm) {
        MerNotice merNotice=new MerNotice();
        merNotice.setIsShow(updateMerNoticeForm.getIsShow());
        merNotice.setNoticeContent(updateMerNoticeForm.getNoticeContent());
        merNotice.setNoticeTitle(updateMerNoticeForm.getNoticeTitle());
        merNotice.setNoticeId(updateMerNoticeForm.getNoticeId());
        try {

            merNoticeMapper.updateByPrimaryKeySelective(merNotice);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }
    //根据商家id查询所有公告
    @Override
    public ResultVO selectAllNoticeByMerId(Integer pageNum,Integer merchantId) {
        List<MerNotice> notices=new ArrayList<>();
        try {
            PageHelper.startPage(pageNum,5);
            notices=merNoticeMapper.selectAllByMerId(merchantId);

            PageInfo pageInfo = new PageInfo(notices);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }


        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(notices);

        return ResultVOUtil.success(hashMap);
    }

    //根据id查询公告
    @Override
    public ResultVO selectNoticeById(Integer noticeId) {
        MerNotice merNotice=new MerNotice();
        try {

            merNotice=merNoticeMapper.selectByPrimaryKey(noticeId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(merNotice);

    }
    //根据商家id以及状态查询公告列表
    @Override
    public ResultVO selectNoticeByMerchantId(Integer merchantId) {
        MerNotice merNotice=new MerNotice();
        merNotice.setIsShow(1);
        merNotice.setMerchantId(merchantId);

        List<MerNotice> notices=new ArrayList<>();
        try {
           // PageHelper.startPage(pageNum,5);
            notices=merNoticeMapper.selectAllowNotices(merNotice);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(notices);
        hashMap.put("list",notices);

        return ResultVOUtil.success(hashMap);
    }

    //删除公告
    @Override
    public ResultVO deleteMerNotice(Integer noticeId) {
        try {

            merNoticeMapper.deleteByPrimaryKey(noticeId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }
}
